package component;

import com.fasterxml.jackson.core.JsonProcessingException;
import dao.EmployerDao;
import listener.ActionDoneListener;
import model.Employer;
import utils.ScreenUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class UpdateEmployerDialog extends JDialog {
    final int WIDTH = 400;
    final int HEIGHT = 360;
    private String id;
    private ActionDoneListener listener;
    EmployerDao employerDao = new EmployerDao();
    private Map<String,Object> map;

    private JTextField nameField;
    private JTextField stockField;
    private JTextField authorField;
    private JTextField priceField;
    private JTextArea descArea;

    public UpdateEmployerDialog(JFrame jf, String title, boolean isModel, ActionDoneListener listener, String id) throws SQLException {
        super(jf,title,isModel);
        Employer employer = employerDao.findById(Integer.parseInt(id));
        this.listener  = listener;
        this.id=id;
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装名称
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLable = new JLabel("员工姓名：");
        JTextField nameField = new JTextField(15);
        nameField.setText(employer.getName());
        nameBox.add(nameLable);
        nameBox.add(Box.createHorizontalStrut(20));
        nameBox.add(nameField);

        //组装年龄
        Box ageBox = Box.createHorizontalBox();
        JLabel ageLable = new JLabel("员工年龄：");
        JTextField ageField = new JTextField(15);
        ageField.setText(employer.getAge());
        ageBox.add(ageLable);
        ageBox.add(Box.createHorizontalStrut(20));
        ageBox.add(ageField);

        //组装性别
        Box gBox = Box.createHorizontalBox();
        JLabel gLabel = new JLabel("员工性别：");
        JRadioButton maleBtn = new JRadioButton("男性",true);
        JRadioButton femaleBtn = new JRadioButton("女性",false);

        //实现单选的效果
        ButtonGroup bg = new ButtonGroup();
        bg.add(maleBtn);
        bg.add(femaleBtn);

        gBox.add(gLabel);
        gBox.add(Box.createHorizontalStrut(20));
        gBox.add(maleBtn);
        gBox.add(femaleBtn);
        gBox.add(Box.createHorizontalStrut(120));


        //组装部门
        Box deptBox = Box.createHorizontalBox();
        JLabel deptLabel = new JLabel("员工部门：");
        JRadioButton aBtn = new JRadioButton("人事 ",true);
        JRadioButton bBtn = new JRadioButton("管理",false);

        //实现单选的效果
        ButtonGroup bg4 = new ButtonGroup();
        bg4.add(aBtn);
        bg4.add(bBtn);

        deptBox.add(deptLabel);
        deptBox.add(Box.createHorizontalStrut(20));
        deptBox.add(aBtn);
        deptBox.add(bBtn);
        deptBox.add(Box.createHorizontalStrut(120));

        //组装婚姻
        Box marriageBox = Box.createHorizontalBox();
        JLabel marriageLabel = new JLabel("婚姻状况：");
        JRadioButton marriedBtn = new JRadioButton("已婚",true);
        JRadioButton singleBtn = new JRadioButton("未婚",false);

        //实现单选的效果
        ButtonGroup bg1 = new ButtonGroup();
        bg1.add(maleBtn);
        bg1.add(femaleBtn);

        marriageBox.add(marriageLabel);
        marriageBox.add(Box.createHorizontalStrut(20));
        marriageBox.add(marriedBtn);
        marriageBox.add(singleBtn);
        marriageBox.add(Box.createHorizontalStrut(120));


        //组装学历
        Box degreeBox = Box.createHorizontalBox();
        JLabel degreeLabel = new JLabel("员工学历：");
        JRadioButton specialtyBtn = new JRadioButton("大专",true);
        JRadioButton bachelorBtn = new JRadioButton("本科",false);
        JRadioButton masterBtn = new JRadioButton("硕士",false);
        //实现单选的效果
        ButtonGroup bg2 = new ButtonGroup();
        bg2.add(specialtyBtn);
        bg2.add(bachelorBtn);
        bg2.add(masterBtn);


        degreeBox.add(degreeLabel);
        degreeBox.add(Box.createHorizontalStrut(20));
        degreeBox.add(specialtyBtn);
        degreeBox.add(bachelorBtn);
        degreeBox.add(masterBtn);
        degreeBox.add(Box.createHorizontalStrut(120));

        //组装职称
        Box titleBox = Box.createHorizontalBox();
        JLabel titleLabel = new JLabel("员工职称：");
        JRadioButton normalBtn = new JRadioButton("普通",true);
        JRadioButton excellentBtn = new JRadioButton("优秀",false);

        //实现单选的效果
        ButtonGroup bg3 = new ButtonGroup();
        bg3.add(maleBtn);
        bg3.add(femaleBtn);

        titleBox.add(titleLabel);
        titleBox.add(Box.createHorizontalStrut(20));
        titleBox.add(normalBtn);
        titleBox.add(excellentBtn);
        titleBox.add(Box.createHorizontalStrut(120));

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton updateBtn = new JButton("修改");
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //获取用户修改后在输入框中输入的内容
                String name = nameField.getText().trim();
                String age = ageField.getText().trim();
                String gender = bg.isSelected(maleBtn.getModel()) ? maleBtn.getText():femaleBtn.getText();
                String dept = bg4.isSelected(aBtn.getModel()) ? aBtn.getText() : bBtn.getText();
                String marriage = bg1.isSelected(marriedBtn.getModel()) ? marriedBtn.getText():singleBtn.getText();
                String degree = bachelorBtn.getText(); // 默认是本科
                if (bg2.isSelected(specialtyBtn.getModel())) {
                    degree = specialtyBtn.getText();
                } else if (bg2.isSelected(masterBtn.getModel())) {
                    degree = masterBtn.getText();
                }
                String  title = bg3.isSelected(normalBtn.getModel()) ? normalBtn.getText():excellentBtn.getText();
                Employer employer = new Employer();
                employer.setId(Integer.parseInt(id));
                employer.setName(name);
                employer.setAge(age);
                employer.setSex(gender);
                employer.setMarriage(marriage);
                employer.setDept(dept);
                employer.setDegree(degree);
                employer.setTitle(title);
                boolean flag = false;
                try {
                    flag = employerDao.updateEmployer(employer);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (flag){
                    //修改成功
                    JOptionPane.showMessageDialog(jf,"修改成功");
                    dispose();
                    try {
                        listener.done(null);
                    } catch (JsonProcessingException ex) {
                        ex.printStackTrace();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }else{
                    //修改失败
                    JOptionPane.showMessageDialog(jf,"修改失败");
                }

            }
        });

        btnBox.add(updateBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(ageBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(gBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(deptBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(marriageBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(degreeBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(titleBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(20));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(20));

        this.add(hBox);

    }



}
